# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html
import datetime
import re
import scrapy
from scrapy.loader import ItemLoader
from scrapy.loader.processors import TakeFirst, MapCompose


class ScrapynewsItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    pass

# 标题定制
def add_jobbole(value):
    return value+"-yanmei"


# 日期转化定制
def date_convert(value):
    try:
        create_date=datetime.datetime.strftime(value,"%Y%m%d").date()
    except Exception as e:
        create_date=datetime.datetime.now().date()

# 配置单独得到数字  去掉文字
def get_nums(value):
    match_re=re.match(".*?(\d+).*",value)
    if match_re:
        nums=int(match_re.group(1))
    else:
        nums=0

def return_value(value):
    return value

# 自定义itemload
class ArticleItemLoader(ItemLoader):
        default_output_processor = TakeFirst()



class JobBoleArticleItem(scrapy.Item):
    title=scrapy.Field()
    create_date=scrapy.Field()
    url=scrapy.Field()
    url_object_id=scrapy.Field()
    front_image_url=scrapy.Field(
        output_processor=MapCompose(return_value)
    )
    front_image_path=scrapy.Field()
    praise_nums=scrapy.Field()
    comment_nums=scrapy.Field(
        input_processor=MapCompose(get_nums)
    )
    fav_nums=scrapy.Field()
    tags=scrapy.Field()
    content=scrapy.Field()


